package com.platform.controller;

import com.platform.entity.SpreadRankEntity;
import com.platform.service.SpreadRankService;
import com.platform.utils.DateUtils;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 排行榜Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-20 19:54:58
 */
@RestController
@RequestMapping("spreadrank")
public class SpreadRankController {
    @Autowired
    private SpreadRankService spreadRankService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("spreadrank:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SpreadRankEntity> spreadrankList = spreadRankService.queryList(query);
        int total = spreadRankService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(spreadrankList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("spreadrank:info")
    public R info(@PathVariable("id") Long id) {
        SpreadRankEntity spreadrank = spreadRankService.queryObject(id);

        return R.ok().put("spreadrank", spreadrank);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("spreadrank:save")
    public R save(@RequestBody SpreadRankEntity spreadrank) {
        spreadRankService.save(spreadrank);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("spreadrank:update")
    public R update(@RequestBody SpreadRankEntity spreadrank) {
        spreadRankService.update(spreadrank);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("spreadrank:delete")
    public R delete(@RequestBody Long[] ids) {
        spreadRankService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 初始历史榜单
     */
    @RequestMapping("/init-history")
    @RequiresPermissions("spreadrank:info")
    public R initHistory() {
        String date = DateUtils.getYYYYMMDDForDate();
        spreadRankService.initHistory(date);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SpreadRankEntity> list = spreadRankService.queryList(params);

        return R.ok().put("list", list);
    }
}
